package com.itheima.core.service.impl;

import com.itheima.common.utils.Page;
import com.itheima.core.dao.CustomerDao;
import com.itheima.core.po.Customer;
import com.itheima.core.service.iservice.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
   @Autowired
   CustomerDao customerdao;

    @Override
    public Page<Customer> findCustomerList(Integer page, Integer rows, String custName, String custSource, String custIndustry, String custLevel) {
        Customer customer = new Customer();

        if(StringUtils.isNotBlank(custName)){
            customer.setCust_name(custName);
        }
        if(StringUtils.isNoneBlank(custSource)){
            customer.setCust_source(custSource);
        }
        if(StringUtils.isNotBlank(custIndustry)){
            customer.setCust_industry(custIndustry);
        }
        if(StringUtils.isNotBlank(custLevel)){
            customer.setCust_level(custLevel);
        }
        System.out.println(customer);


        customer.setStart((page-1) * rows);
        customer.setRows(rows);
        List<Customer> customers = customerdao.selectCustomerList(customer);
        System.out.println(customers);
        Integer count = customerdao.selectCustomerListCount(customer);
        Page<Customer> result = new Page<>();
        result.setPage(page);

        result.setRows(customers);
        result.setSize(rows);
        result.setTotal(count);
        return result;

    }

    @Override
    public List<Customer> findByCustId(Integer id) {

        List<Customer> customers = customerdao.selectCustomerById(id);
        return customers;
    }

    @Override
    public int deleteById(Integer id) {
        int index = customerdao.deleteById(id);
        return index;
    }

    @Override
    public int createCustomer(Customer customer) {
        int index = customerdao.createCustomer(customer);
        return index;
    }

}
